package com.wpw.java8demo;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.util.TimeZone;
import java.util.concurrent.SynchronousQueue;

import org.junit.Test;

public class DateTest {

	@Test
	public void test1() {
		LocalDate date1 = LocalDate.of(2017, 7, 6);

		date1 = LocalDate.now();

		System.out.println(date1);
		System.out.println(date1.getYear());
		System.out.println(date1.getMonthValue());
		System.out.println(date1.getMonth());
		System.out.println(date1.getDayOfMonth());

		System.out.println(date1.getDayOfWeek());
		System.out.println(date1.lengthOfMonth());
		System.out.println(date1.lengthOfYear());

	}

	@Test
	public void test2() {
		LocalDate date1 = LocalDate.now();
		
		System.out.println(date1.get(ChronoField.YEAR));
		System.out.println(date1.get(ChronoField.MONTH_OF_YEAR));
		System.out.println(date1.get(ChronoField.DAY_OF_MONTH));
	}

	@Test
	public void test3() {
		//超过会报错
		LocalTime time = LocalTime.of(16, 40, 40);
		
		
		time = LocalTime.now();
		System.out.println(time);
		System.out.println(time.getHour());
		System.out.println(time.getMinute());
		System.out.println(time.getSecond());
		System.out.println(time.getNano());
		
		System.out.println("=============");
		System.out.println(time.get(ChronoField.HOUR_OF_DAY));
	}
	
	@Test
	public void test4() {
		LocalDate date1 = LocalDate.parse("2018-08-08");
		System.out.println(date1);
		
		LocalDate date2 = LocalDate.parse("2018/08/08", DateTimeFormatter.ofPattern("yyyy/MM/dd"));
		System.out.println(date2.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
		
		LocalTime time = LocalTime.parse("12:12:12");
		System.out.println(time);
	}
	
	@Test
	public void test5() {
		LocalDateTime dateTime = LocalDateTime.now();
		System.out.println(dateTime);
		System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		
		System.out.println("===========1111111111111111");
		LocalDateTime dateTime2 = LocalDateTime.of(2018, 3, 3, 12, 50, 50);
		System.out.println(dateTime2);

		System.out.println("===========2222222222222222");
		LocalDate localDate = dateTime2.toLocalDate();
		System.out.println(localDate);
		LocalTime localTime = dateTime2.toLocalTime();
		System.out.println(localTime);
		
		
	}
	
	/**
	 * 机器的日期和时间格式
	 * @Description：
	 * @author: wupengwei
	 * @date: 2018年7月8日 上午11:54:12
	 */
	@Test
	public void test6() {
		Instant now = Instant.now();
		System.out.println(now);

		Instant ofEpochMilli = Instant.ofEpochSecond(60);
		System.out.println(ofEpochMilli);//1970-01-01T00:01:00Z
		
		System.out.println(now.get(ChronoField.YEAR));
	}
	
	@Test
	public void test7() {
		LocalDateTime dateTime1 = LocalDateTime.parse("2018-01-01 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		LocalDateTime dateTime2 = LocalDateTime.now();
		Duration duration = Duration.between(dateTime1, dateTime2);
		System.out.println(duration);
	}
	
	@Test
	public void test8() {
		LocalDateTime now = LocalDateTime.now();
		LocalDateTime d2017 = now.withYear(2017);
		System.out.println(d2017);
		
		LocalDateTime m2018 = now.with(ChronoField.MONTH_OF_YEAR, 1);
		System.out.println(m2018);
		
		LocalDateTime plusDays = now.plusDays(2);
		System.out.println(plusDays);
	}
	
	@Test
	public void test9() {
		ZoneId zoneId = TimeZone.getDefault().toZoneId();
		System.out.println(zoneId);
		
		ZoneId romeZone = ZoneId.of("Europe/Rome");
		LocalDate d1 = LocalDate.of(2014, 3, 3);
		d1.atStartOfDay(romeZone);
		System.out.println(d1);
		
		Instant now = Instant.now();
		ZonedDateTime atZone = now.atZone(romeZone);
		System.out.println(atZone);
	}
}
